#!/bin/bash
#author:wuwentao
#time:2023.4.12

set -e

#参数赋值
num=$1
branch1="master"
branch2="OpenHarmony-3.2-beta2"
branch3="OpenHarmony-3.2-beta2-merge"
branch4="OpenHarmony-3.2-release"

#参数转换
function num_parameter(){
	if [ "$num"x = "1.1"x ];then
		path="$branch1"
	elif [ "$num"x = "1.2"x ];then
		path="$branch2"
	elif [ "$num"x = "1.3"x ];then
		path="$branch3"
	elif [ "$num"x = "1.4"x ];then
		path="$branch4"
	fi	
}

#参数转换
function zhuanhuan(){
	if [ "$num"x = "$branch1"x ];then
num="1.1"
	elif [ "$num"x = "$branch2"x ];then
num="1.2"
	elif [ "$num"x = "$branch3"x ];then
num="1.3"
	elif [ "$num"x = "$branch4"x ];then
num="1.4"
	fi	
}

#打印菜单
function open_menu(){
echo "**********************************************"
echo "* Welcome to download OpenHarmony Code       *"
echo "* Please Choice OpenHarmony SDK:             *"
echo "* OpenHarmony Version                        *"
echo "* OpenHarmony master              prese 1.1  *"
echo "* OpenHarmony-3.2-beta2           press 1.2  *"
echo "* OpenHarmony-3.2-beta2-merge     press 1.3  *"
echo "* OpenHarmony-3.2-release         press 1.4  *"
echo "**********************************************"
echo
read -p "input version:" num
echo
path=$num
download_hm
over_download
exit 0
}

#创建路径
function create_path(){
if [ ! -e "$path" ]; then
    mkdir "$path"
    cd "$path"
elif [ ! -e "$path"_temp ]; then
    mkdir "$path"_temp
    cd "$path"_temp
else
    rm -rf "$path"_temp
    mkdir "$path"_temp
    cd "$path"_temp
fi
}

#打印下载结果
function over_download(){
    echo "you have been download OpenHarmony $path code"
    echo
    echo "    url       : https://gitee.com/openharmony/manifest"
    echo "    branch    : $path"
    echo "    xml_name  : ${x_name}"
    echo "    code_path : $(pwd)"
    echo "    init   cmd: repo init -u https://gitee.com/openharmony/manifest.git -b $version ${_m} ${default_xml} --no-repo-verify"
    echo "    sync   cmd: repo sync -c"
    echo "    lfs    cmd: repo forall -c 'git lfs pull'"
    echo "    set_br cmd: repo start $version  --all"
}

#下载程序(加了echo直接打印出来来模拟下载)
function download_hm(){
if [ "$num"x = '1.1'x ]; then
    x_name=""
    version=master
    _m=""
    default_xml=""
	  num_parameter
    create_path	
    echo '    repo init -u https://gitee.com/openharmony/manifest.git -b master --no-repo-verify
    repo sync -c
    repo forall -c 'git lfs pull'
    repo start master --all'
    echo
elif [ "$num"x = '1.2'x ]; then
    version=OpenHarmony-3.2-Beta2
    _m=""
    default_xml=""
	  num_parameter
    create_path
    echo '    repo init -u https://gitee.com/openharmony/manifest -b OpenHarmony-3.2-Beta2 --no-repo-verify
    repo sync -c
    repo forall -c 'git lfs pull'
    repo start OpenHarmony-3.2-Beta2 --all'
    echo
elif [ "$num"x = '1.3'x ]; then
    x_name="thead_develop.xml"
    version=OpenHarmony-3.2-beta2-Merge
    _m="-m"
    default_xml="${x_name}"
	  num_parameter 
    create_path
    echo '    repo init -u https://gitee.com/openharmony/manifest -b OpenHarmony-3.2-beta2-Merge -m thead_develop.xml --no-repo-verify
    repo sync -c
    repo forall -c 'git lfs pull' 
    repo start OpenHarmony-3.2-beta2 --all'
    echo
    version=OpenHarmony-3.2-beta2
elif [ "$num"x = '1.4'x ]; then
    x_name="default.xml" 
    version=OpenHarmony-3.2-Release
    _m="-m"
    default_xml="default.xml"
	  num_parameter
    create_path     
    echo '    repo init -u https://gitee.com/openharmony/manifest -b OpenHarmony-3.2-Release -m default.xml --no-repo-verify
    repo sync -c
    repo forall -c 'git lfs pull'
    repo start  OpenHarmony-3.2-Release --all'
    echo
elif [ -z $num ];then
open_menu
else 
    echo 'product is not in product list, please recheck!!!'
    open_menu
fi
}

#主函数
function main(){
zhuanhuan
num_parameter
download_hm
over_download
}
main